package com.briup.jz.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.jz.bean.AccountApply;
import com.briup.jz.bean.AccountApplyExample;
import com.briup.jz.bean.extend.AccountApplyExtend;
import com.briup.jz.dao.AccountApplyMapper;
import com.briup.jz.dao.extend.AccountApplyExtendMapper;
import com.briup.jz.service.AccountApplyService;
import com.briup.jz.utils.CustomerException;
@Service
public class AccountApplyServiceImpl implements AccountApplyService{

	@Autowired
	private AccountApplyMapper accountapplyMapper;
	
	@Autowired
	private AccountApplyExtendMapper accountapplyExtendMapper;
	
	@Override
	public void saveOrUpdate(AccountApply accountapply) throws CustomerException {
	
		if(accountapply.getId()!=null) {
			accountapplyMapper.updateByPrimaryKey(accountapply);
		}else {
//			// 判断是否有同名，如果有抛出异常
//			AccountApplyExample example = new AccountApplyExample();
//			example.createCriteria().andIdEqualTo(AccountApply.getId());
//			List<AccountApply> list = accountapplyMapper.selectByExample(example);
//			if(list.size()>0) {
//				throw new CustomerException("同名已存在");
			/*applyTime=new Date();
			accountapply.setApplyTime(applyTime);*/

			// 初始化
			accountapply.setApplyTime(new Date().getTime());
				/*		article.setReadTimes(0l);
						article.setThumpUp(0l);*/
			accountapply.setStatus("未审核");
			accountapplyMapper.insert(accountapply);
			}
	}

	@Override
	public List<AccountApply> query(Long id,String status,String applyType,Long userId) {
	/*	模板查询*/
		AccountApplyExample example=new AccountApplyExample();
		/*条件*/
		AccountApplyExample.Criteria criteria = example.createCriteria();
		if(id!=null) {
			criteria.andIdEqualTo((long)id);
		}
		if(status!=null) {
			criteria.andStatusEqualTo(status);
		}
		if(applyType!=null) {
			criteria.andApplyTypeEqualTo(applyType);
		}
		if (userId!=null) {
			criteria.andUserIdEqualTo(userId);
		}
		List<AccountApply> list = accountapplyMapper.selectByExample(example);
		
		/*for(AccountApply a:list) {
			System.out.println(a);
		}*/
		return list;
	}

	@Override
	public void deleteById(long id) throws CustomerException {
		// TODO Auto-generated method stub
		AccountApply address=accountapplyMapper.selectByPrimaryKey(id);
		if(address==null) {
			throw new CustomerException("要删除不存在");
		}
		accountapplyMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void checkPass(Long id) throws CustomerException {
		AccountApply a=new AccountApply();
		String status="通过";
		a.setStatus(status);
		a.setId(id);
		accountapplyMapper.updateByPrimaryKeySelective(a);
		
	}

	@Override
	public void checkNoPass(Long id, String reason) throws CustomerException {
		AccountApply a=new AccountApply();
		String status="不通过";
		a.setStatus(status);
		a.setId(id);
		a.setReason(reason);
		accountapplyMapper.updateByPrimaryKeySelective(a);
	}
//    级联多符合查询
	@Override
	public List<AccountApplyExtend> queryCascade(String status, String applyType, Long userId) {
		return accountapplyExtendMapper.select(status,applyType, userId);

	}

	@Override
	public void revokeAccountApply(Long id) {
		AccountApply a=new AccountApply();
		String status="未审核";
		a.setStatus(status);
		a.setId(id);
		accountapplyMapper.updateByPrimaryKeySelective(a);
	}
}
